# -*- coding: utf-8 -*-

"""
Date: 2023/2/23 17:01
Author: Darker
"""
import logging
import sys
from logging import handlers


class Log(object):

    def __new__(cls, *args, **kwargs):
        super().__new__(cls, *args, **kwargs)
        logger = logging.getLogger("ZTCCLOUD_DaliyReport_TAPD")
        logger.level = logging.DEBUG
        cls.addHandler = logger.addHandler
        cls.__config_logger_object()
        return logger

    @classmethod
    def __config_logger_object(cls):
        from config import conf

        # 日志文件日志输出
        # 日志格式
        formatter = "%(asctime)s %(levelname)s [%(name)s] %(filename)s/%(funcName)s:%(lineno)d --- [%(threadName)s]: " \
                    "%(message)s"
        fileFormatter = logging.Formatter(formatter)
        # 记录全量日志
        fullHandler = handlers.TimedRotatingFileHandler(filename=conf.runLogPath,
                                                        when="d",
                                                        backupCount=3,
                                                        encoding="UTF-8")
        fullHandler.setFormatter(fileFormatter)
        fullHandler.setLevel(logging.DEBUG)
        # 记录错误日志
        errorHandler = logging.FileHandler(conf.errorLogPath, encoding="UTF-8")
        errorHandler.setFormatter(fileFormatter)
        errorHandler.setLevel(logging.ERROR)

        # 控制台日志输出
        streamHandler = logging.StreamHandler(stream=sys.stdout)
        formatter = "%(asctime)s %(levelname)s: %(message)s"
        streamFormatter = logging.Formatter(formatter)
        streamHandler.setFormatter(streamFormatter)
        streamHandler.setLevel(logging.DEBUG)

        # 添加日志handler
        cls.addHandler(fullHandler)
        cls.addHandler(errorHandler)
        cls.addHandler(streamHandler)


logger = Log()

if __name__ == '__main__':
    logger.debug("123123123")
